package cn.truistic.enmicromsg.wx.wx_nearby;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.truistic.enmicromsg.R;
import cn.truistic.enmicromsg.base.BaseActivity;
import cn.truistic.enmicromsg.common.dialog.ContentDialog;
import cn.truistic.enmicromsg.common.interfaces.DialogContentCallback;
import cn.truistic.enmicromsg.common.util.PreferenceHelper;
import cn.truistic.enmicromsg.common.util.WxHeple;

/**
 * 用途;一键添加附近的人
 * 时间：09点38分
 * name: ts
 */

public class NearbyActivity extends BaseActivity implements DialogContentCallback {
    private Toolbar mToolbar;
    private Button but_begin; // 开启辅助自动对附近人大招呼
    private Button but_add_content; //设置打招呼的内容
    private EditText mEtContent;
    private TextView tv_greeting_content;
    private ContentDialog contentDialog;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_nearby );
        initView();
        initListener();
    }

    //监听
    private void initListener() {
        but_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = mEtContent.getText().toString().trim();     //输入人数
                int  mNearTextNum = 0;
                try {
                     mNearTextNum = Integer.parseInt(number);    //String转化成Int
                } catch ( NumberFormatException e ) {
                    Toast.makeText(mContext, "输入有误，重新输入..", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mNearTextNum > 80 ) {
                    Toast.makeText(mContext, "每次添加上限80人哦~", Toast.LENGTH_SHORT).show();
                    return;
                }

                boolean isOpen = isAccessibilitySettingsOn();
                if (isOpen){
                    final int finalMNearTextNum = mNearTextNum;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            PreferenceHelper.commitInt( "mNearTextNum" , finalMNearTextNum ); //存本地附近的人的数量一次最多80
                            mEnApplication.setNearbyPeople(true); //开启附近的人的模块
                            mEnApplication.setAllowGreeting(true);
                            WxHeple.execShellCmd("input keyevent 3");
                            WxHeple.execShellCmd("am force-stop com.tencent.mm");
                            sleepMiss( 2500 );
                            WxHeple.intentWechat(mContext);//开启微信
                        }
                    }).start();
                }
            }
        });

        but_add_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                contentDialog.show();
            }
        });
    }

    //初始化空间
    private void initView() {
        setSupportActionBar(mToolbar);
        mToolbar = (Toolbar) findViewById( R.id.mToolbar );
        but_begin = (Button) findViewById( R.id.but_begin );
        but_add_content= (Button) findViewById( R.id.but_edit );
        mEtContent = (EditText) findViewById( R.id.et_content );
        tv_greeting_content = (TextView) findViewById( R.id.tv_greeting_content );
        contentDialog = new ContentDialog(NearbyActivity.this, NearbyActivity.this);
        String content = PreferenceHelper.getString("ContentDialogText" ,"");
        if (!TextUtils.isEmpty(content)){
            tv_greeting_content.setText("打招呼内容: "+ content);
        }
    }

    @Override
    public void Negative() {
        contentDialog.cancel(); //取消
    }

    @Override
    public void Positive( String content ) {
        tv_greeting_content.setText( "打招呼内容: "+content );
        PreferenceHelper.commitString( "ContentDialogText", content ); //存content 带sp
    }

}
